import { query,queryPress,queryRevenue,queryMeter,queryWorkstations,queryWaterworks,queryWaterworksHistory } from '@/services/dashboard';
import uuid from 'uuid-js';
import moment from 'moment'
export default {
  namespace: 'dashboard',
  state: {
    pageLoaded:false,
    data:{
      date_range_charge_analysis:[],
      date_range_open_account_analysis:[],
      user_analysis:[],
    },
    zoom:1.2,
    scrollData:{x:0,y:0},
    uuid: uuid.create(),
    searchParams:{
      date:moment().format("YYYY-MM-DD"),
    },
    pressData:[],
    MeterData:{
      summary:{},
      date_range_consumption:[],
      date_range_status_analysis:[],
      vendor_analysis:[],
      consumption_ranking:[],
      focus_meter_analysis:[]
    },
    RevenueData:{
      date_range_charge_analysis:[],
      date_range_open_account_analysis:[],
      user_analysis:[],
    },
    WorkstationsData:[],
    WaterworksData:{},
    WaterworksHistoryData:[],
    loading: true,
  },
  effects: {
    *saveZoom({ payload, callback }, { call, put }) {
      yield put({
        type: 'saveZoomData',
        payload: payload,
      });
    },
    *reset({ payload, callback }, { call, put,select }) {
      // const state = yield select(state => state.homeData)//获取state中的数据
      // let uuid=state.uuid;
      // console.log('uuid',uuid)
      // yield put({
      //   type: 'addUUid',
      //   payload:uuid+1
      // });
     console.log('reset')
      yield put({
        type: 'resetData',
        payload:payload
      });

      if(callback) callback()
    },
    *changeDate({ payload, callback }, { call, put }) {
      yield put({
        type: 'saveDate',
        payload: payload,
      });
      if(callback) callback()
    },
    *changeLoaded({ payload, callback }, { call, put }) {
      yield put({
        type: 'changePageLoaded',
        payload: true,
      });
    },
    *changeScroll({ payload, callback }, { call, put }) {
      console.log('scroll payload',payload)
      yield put({
        type: 'changeScrollData',
        payload: payload,
      });
    },
    *fetch({ payload,callback }, { call, put ,select}) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const {searchParams} = yield select(state => state.dashboard)
      const response = yield call(query, {...searchParams});
      const response2 = yield call(queryPress, payload);
      const response3 = yield call(queryMeter, payload);
      const response4 = yield call(queryRevenue, payload);
      const response5 = yield call(queryWorkstations, payload);
      const response6 = yield call(queryWaterworks, payload);
      const response7 = yield call(queryWaterworksHistory, payload);

      if(response.status===200){
        yield put({
          type: 'save',
          payload: response.data,
          uuid: uuid.create(),
        });
        yield put({
          type: 'savePress',
          payload:  response2.data
        });
        yield put({
          type: 'saveWorkstations',
          payload:  response5.data
        });
        yield put({
          type: 'saveMeter',
          payload:  response3.data
        });
        yield put({
          type: 'saveRevenue',
          payload:  response4.data
        });
        yield put({
          type: 'saveWaterworks',
          payload:  response6.data.data
        });
        yield put({
          type: 'saveWaterworksHistory',
          payload:  response7.data.data
        });
        yield put({
          type: 'changeLoading',
          payload: false,
        });
        if (callback) callback();
      }

    },
  },
  *fetchPress({ payload,callback }, { call, put ,select}) {
    const response = yield call(queryPress, payload);
    console.log(response)


  },
  reducers: {
    saveZoomData(state, action) {
      return {
        ...state,
        zoom: action.payload,
      };
    },
    save(state, action) {
      return {
        ...state,
        data: action.payload,
        uuid: action.uuid
      };
    },
    savePress(state, action) {
      return {
        ...state,
        pressData: action.payload.data,
      };
    },
    saveWorkstations(state, action) {
      return {
        ...state,
        WorkstationsData: action.payload.data,
      };
    },
    saveMeter(state, action) {
      return {
        ...state,
        MeterData: action.payload,
      };
    },
    saveRevenue(state, action) {
      return {
        ...state,
        RevenueData: action.payload,
      };
    },
    saveWaterworks(state, action) {
      return {
        ...state,
        WaterworksData: action.payload,
      };
    },
    saveWaterworksHistory(state, action) {
      return {
        ...state,
        WaterworksHistoryData: action.payload,
      };
    },
    saveDate(state, action) {
      return {
        ...state,
        searchParams:{
          ...state.searchParams,
          date:action.payload
        }
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
    changePageLoaded(state, action) {
      return {
        ...state,
        pageLoaded: action.payload,
      };
    },
    changeScrollData(state, action) {
      return {
        ...state,
        scrollData: action.payload,
      };
    },
    addUUid(state, action) {
      return {
        ...state,
        uuid: action.payload,
      };
    },
    resetData(state, action) {
      return  {
        pageLoaded:action.payload,
        data:{
          date_range_charge_analysis:[],
          date_range_open_account_analysis:[],
          user_analysis:[],
        },
        scrollData:{x:0,y:0},
        uuid:0,
        searchParams:{
          date:moment().format("YYYY-MM-DD"),
          license_plate_number:'',
          mobile:'',
          status:'0',
          page: 1,
          per_page: 30,
        },
        pressData:[],
        MeterData:{
          summary:{},
          date_range_consumption:[],
          date_range_status_analysis:[],
          vendor_analysis:[],
          consumption_ranking:[],
          focus_meter_analysis:[]
        },
        RevenueData:{
          date_range_charge_analysis:[],
          date_range_open_account_analysis:[],
          user_analysis:[],
        },
        WorkstationsData:[],
        WaterworksData:{},
        WaterworksHistoryData:[],
        loading: true,
      };
    },
  },

};
